package com.lanou.zakerproject.subscribe;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.zakerproject.R;

/**
 * Created by dllo on 16/1/14.
 */
public class NewsAdapter extends BaseAdapter {
    private Context context;
    private NewsChannelToDetailData newsChannelToDetailData;

    public NewsAdapter(Context context, NewsChannelToDetailData newsChannelToDetailData) {
        this.context = context;
        this.newsChannelToDetailData = newsChannelToDetailData;
    }

    @Override
    public int getCount() {
        return newsChannelToDetailData.getData().getArticles().size();
    }

    @Override
    public Object getItem(int position) {
        return newsChannelToDetailData.getData().getArticles().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_subscribe_news,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        if (newsChannelToDetailData != null){
            myViewHolder.titleTv.setText(newsChannelToDetailData.getData().getArticles().get(position).getTitle());
            myViewHolder.dateTv.setText(newsChannelToDetailData.getData().getArticles().get(position).getAuther_name());
        }
        return convertView;
    }


    class MyViewHolder{
        private TextView titleTv,dateTv;
        public MyViewHolder(View view) {
            titleTv = (TextView) view.findViewById(R.id.item_subscribe_news_title);
            dateTv = (TextView) view.findViewById(R.id.item_subscribe_news_date);
        }
    }
}
